import { getBanners } from '@/api/discover'
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit'
import * as Type from '@/api/discover/type'

export const fetchBannerDataAction = createAsyncThunk(
  'banners',
  (arg, { dispatch }) => {
    // 获取banner图
    getBanners().then((res) => {
      dispatch(changeBannersAction(res.banners))
    })
  }
)

interface ISatate {
  banners: Type.reqBannerFace[]
}

const initialState: ISatate = {
  banners: []
}

const recommendSlice = createSlice({
  name: 'recommend',
  initialState,
  reducers: {
    changeBannersAction(state, { payload }) {
      state.banners = payload
    }
  }
})

export const { changeBannersAction } = recommendSlice.actions
export default recommendSlice.reducer
